package com.gdth.bank.adapter;

import java.io.IOException;
import java.util.Date;

import com.gdth.bank.R;
import com.gdth.bank.bean.DownloadBean;
import com.gdth.bank.model.DownloadModel;
import com.gdth.bank.widget.image.Bimp;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.utils.ToolUtil;
import com.gdth.utils.UUIDUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class PhoneGridAdapter extends BaseAdapter{
	private Context mContext;
	private LayoutInflater inflater; // 视图容器
	private int selectedPosition = -1;// 选中的位置
	private boolean shape;
	private String  scbs;

	public PhoneGridAdapter(Context context) {
		inflater = LayoutInflater.from(context);
		this.mContext = context;
	}
	public PhoneGridAdapter(Context context,String scbs) {
		inflater = LayoutInflater.from(context);
		this.mContext = context;
		this.scbs= scbs;
	}

	public Object getItem(int arg0){
		return null;
	}

	public long getItemId(int arg0) {
		return arg0;
	}
	
	public int getCount() {
		int num = Bimp.getInstance().getDrr().size();
		return (num + 1);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
		
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_phone_gridview, parent, false);
			holder = new ViewHolder();
			holder.image = (ImageView) convertView.findViewById(R.id.item_grida_image);
			holder.imageCheckBox = (ImageView) convertView.findViewById(R.id.item_checkBox);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.image.setVisibility(View.VISIBLE);

		if (position == Bimp.getInstance().getDrr().size()) {
			holder.image.setImageBitmap(BitmapFactory.decodeResource(
					mContext.getResources(), R.drawable.icon_addpic_unfocused));
		} else {
			if (Bimp.getInstance().getDrr().size() == Bimp.getInstance().getBmp().size()) {
				TAppFjxxb fjxx = Bimp.getInstance().getDrr().get(position);
				
				Bitmap bm = Bimp.getInstance().getBmp().get(position);
				if (bm == null) {
	
					TAppFjxxb mTAppFjxxb = Bimp.getInstance().getDrr().get(position);
					String fullPath = ToolUtil.getFileFullPath(ToolUtil.getFilePath(mTAppFjxxb.getUuid() + "." +mTAppFjxxb.getWjlx()));
					try {
						bm = Bimp.revitionImageSize(fullPath);
						Bimp.getInstance().getBmp().set(position, bm);
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (bm == null) {
						DownloadBean bean = new DownloadBean();
						bean.setUuid(mTAppFjxxb.getUuid());
						bean.setLocalPath(mTAppFjxxb.getAppdz());
						bean.setGnmk(mTAppFjxxb.getGnmk());
						bean.setCjsj(new Date());
						DownloadModel.getInstance().download(bean, null, handler);
					}
				} else {
					if(fjxx.getSczt() == 1)
						holder.imageCheckBox.setVisibility(View.VISIBLE);
					
					holder.image.setImageBitmap(bm);
				}
			}
		}
		
		if (position == 9) {
			holder.image.setVisibility(View.GONE);
		}
		
		/*if(scbs != null && scbs.equals("1") && position == Bimp.getInstance().getMax()){
			
			holder.image.setVisibility(View.GONE);
		}*/
		if(position ==Bimp.getInstance().getMax()){
			holder.imageCheckBox.setVisibility(View.GONE);
		}

		return convertView;
	}
	
	public class ViewHolder {
		public ImageView image;
		public ImageView imageCheckBox;
	}
	
	public void setSelectedPosition(int position) { selectedPosition = position;}
	public int getSelectedPosition() { return selectedPosition;}
	
	public boolean isShape() { return shape; }
	public void setShape(boolean shape) { this.shape = shape; }

	public void update() {
		new Thread(new Runnable() {
			public void run() {
				while (true) {
					if (Bimp.getInstance().getMax() == Bimp.getInstance().getCount()) {
						Message message = new Message();
						message.what = 1;
						handler.sendMessage(message);
						break;
					} else {
						try {
							int number = Bimp.getInstance().getMax();
							TAppFjxxb mTAppFjxxb = Bimp.getInstance().getDrr().get(number);
							
							String path = mTAppFjxxb.getAppdz();
							
							if (mTAppFjxxb.getSczt() == -1) {
								//状态为-1代表该数据从图片选择器选取
								Bitmap bm = Bimp.revitionImageSize(path);
								Bimp.getInstance().getBmp().add(bm);
								
								mTAppFjxxb = initFjxx(mTAppFjxxb, path);
								
								String fullPath = ToolUtil.getFileFullPath(mTAppFjxxb.getAppdz());
								ToolUtil.copyFile(path, fullPath, true);
							} else if (mTAppFjxxb.getSczt() == 0) {
								//状态为0代表该图片已在缓存目录
								Bitmap bm = Bimp.revitionImageSize(ToolUtil.getFileFullPath(path));
								Bimp.getInstance().getBmp().add(bm);
							} else if (mTAppFjxxb.getSczt() == 1) {
								//状态为1代表该图片以上传
								Bitmap bm = Bimp.revitionImageSize(ToolUtil.getFileFullPath(path));
								Bimp.getInstance().getBmp().add(bm);
							}
							
							Bimp.getInstance().addMax(1);
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}).start();
	}
	
	/**
	 * 调用线程更新
	 */
	Handler handler = new Handler(new Handler.Callback() {
		
		@Override
		public boolean handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
				case 1:
					notifyDataSetChanged();
					break;
				case 101:
					notifyDataSetChanged();
					break;
			}
			
			return false;
		}
	});
	
	private TAppFjxxb initFjxx(TAppFjxxb mTAppFjxxb, String path) {
		//创建文件名
		String uuid = UUIDUtil.getUUID();
		String wjlx = path.substring(path.lastIndexOf(".") + 1, path.length());
		
		//保存路径,不带sd卡目录
		String savePath = ToolUtil.getFilePath(uuid + "." + wjlx);
		
		mTAppFjxxb.setAppdz(savePath);
		mTAppFjxxb.setWjmc(path.substring(path.lastIndexOf("/") + 1, path.length()));
		mTAppFjxxb.setWjlx(wjlx);
		mTAppFjxxb.setUuid(uuid);
		//更改为待上传
		mTAppFjxxb.setSczt(0);
		
		return mTAppFjxxb;
	}
}